package opc.read.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @Description: 用simu软件 建立的四个信号，这里使用异步读取的测试，
 * @author: liyuan
 * @data 2020-12-27 09:58
 */
public class ReadHostProt {
    public static Map<String, String> trueHostProp() throws IOException {
        InputStream is = ReadHostProt.class.getClassLoader().getResourceAsStream("trueHost.properties");
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        Properties properties = new Properties();
        properties.load(br);
        String host = properties.getProperty("host");
        String domain = properties.getProperty("domain");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String clsid = properties.getProperty("clsid");
        Map<String, String> map = new HashMap<String, String>();
        map.put("host", host);
        map.put("domain", domain);
        map.put("user", user);
        map.put("password", password);
        map.put("clsid", clsid);
        return map;
    }

    public static Map<String, String> loadFile() throws IOException {
        InputStream is = ReadHostProt.class.getClassLoader().getResourceAsStream("config.properties");
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        Properties properties = new Properties();
        properties.load(br);
        String host = properties.getProperty("host");
        String domain = properties.getProperty("domain");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String clsid = properties.getProperty("clsid");
        int len = Integer.parseInt(properties.getProperty("len"));
        Map<String, String> map = new HashMap<String, String>(len * 3 + 4);
        map.put("host", host);
        map.put("domain", domain);
        map.put("user", user);
        map.put("password", password);
        map.put("clsid", clsid);
        map.put("len", properties.getProperty("len"));
        for (int i = 0; i < len; i++) {
            String deviceName = properties.getProperty("deviceName" + i);
            String itemName = properties.getProperty("deviceName" + i + ".deviceItem");
            map.put("deviceName" + i, deviceName);
            map.put("deviceName" + i + ".deviceItem", itemName);
            /***
             * 配置文件少了.pv的适配
             */
//            String[] split = itemName.split(",");
//            StringBuilder builder=new StringBuilder();
//            for (int j = 0; j < split.length-1; j++) {
//                builder.append(split[j]+".pv,");
//            }
//            builder.append(split[split.length-1]+".pv");
//            map.put("deviceName" + i + ".deviceItem",builder.toString());
        }


        return map;
    }

}
